// GLFrame.cpp
//
#include "GLFrame.h"

#include "GlobalResources.h"
#include "MainWindow.h"
#include "GraphicScene.h"

#include <QGraphicsView>
#include <QMouseEvent>
#include <QGLWidget>
#include <string>

#include "MemCheck.h"

using namespace IDS::IDEAS::OpenGLView::Fwk;


//-------------------------------------------------------------------------------------------------
GLFrame::GLFrame( MainWindow *_mainWindow )
: mainWindow(_mainWindow)
{
	glContext	 = new QGLWidget( GlobalResources::getSharingContext()->format(), 0, GlobalResources::getSharingContext()/*QGLFormat(QGL::SampleBuffers)*/ );
	setViewport(glContext);

	glewContext  = new GLEWContext;
	this->makeCurrent();

	glewExperimental = GL_TRUE;
	glewInit();

	//graphicScene = scene ? scene : new OpenGLView::GraphicScene2D( mainWindow );

	width  = glContext->size().width();
	height = glContext->size().height();
	
	
	//setOptimizationFlag(QGraphicsView::DontSavePainterState);
	//setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing);

	//setViewportUpdateMode( QGraphicsView::SmartViewportUpdate );
	setViewportUpdateMode( QGraphicsView::FullViewportUpdate );
	//setViewportUpdateMode( QGraphicsView::MinimalViewportUpdate );
	//setViewportUpdateMode( QGraphicsView::NoViewportUpdate );
	
	//setCacheMode( QGraphicsView::CacheBackground );
	//setCacheMode( QGraphicsView::CacheNone );


	//setScene(graphicScene);
}

//-------------------------------------------------------------------------------------------------
// Destructor
GLFrame::~GLFrame()
{
	//if(graphicScene)
	//{
	//	delete graphicScene;
	//	graphicScene = NULL;
	//}

	if(glewContext)
	{
		if (this->glewContext == glewGetContext())
			glewSetContext(NULL);

		delete glewContext;
		glewContext = NULL;
	}
}

//-------------------------------------------------------------------------------------------------
bool GLFrame::init()
{
	setMouseTracking(true); 
	return true;
}

//-------------------------------------------------------------------------------------------------
void GLFrame::resizeEvent( QResizeEvent *event )
{
	width  = event->size().width();
	height = event->size().height();

	GraphicScene *gs = dynamic_cast<GraphicScene*>( scene() );
	
	if(gs)	gs->resizeEvent(event);

	QGraphicsView::resizeEvent(event);
}

//-------------------------------------------------------------------------------------------------
void GLFrame::wheelEvent( QWheelEvent *e )
{
	QGraphicsView::wheelEvent( e );
}

//-------------------------------------------------------------------------------------------------
void GLFrame::mousePressEvent( QMouseEvent *e )
{
	QGraphicsView::mousePressEvent( e );
}

//-------------------------------------------------------------------------------------------------
void GLFrame::mouseMoveEvent( QMouseEvent *e )
{ 
	QGraphicsView::mouseMoveEvent( e );
}

//-------------------------------------------------------------------------------------------------
void GLFrame::mouseReleaseEvent( QMouseEvent *e )
{
	QGraphicsView::mouseReleaseEvent( e );
}

//-------------------------------------------------------------------------------------------------
void GLFrame::enterEvent( QEvent *e )
{
	QGraphicsView::enterEvent( e );
	//graphicScene->setFocus();
}

//-------------------------------------------------------------------------------------------------
void GLFrame::leaveEvent( QEvent *e )
{
	QGraphicsView::leaveEvent( e );
	GraphicScene *gs = dynamic_cast<GraphicScene*>( scene() );

	if( gs )
	{
		gs->hideToolTip();
		gs->hideContextMenu();
		gs->hilightObject(NULL);
		gs->selectionSet.setPolicy(PolicyNormal);
		gs->showSnapPoint(false);

		gs->invalidate();
	}
}

//-------------------------------------------------------------------------------------------------
void GLFrame::makeCurrent()
{
	glContext->makeCurrent();
	glewSetContext(this->glewContext);
}


//-------------------------------------------------------------------------------------------------
void GLFrame::setCursor( const QCursor &cur )
{
	glContext->setCursor( cur );
}

//-------------------------------------------------------------------------------------------------
//bool GLFrame::eventFilter(QObject *obj, QEvent *event)
//{
//	if (event->type() == QEvent::KeyPress)
//	{
//		QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
//		printf("Ate key press %d\n", keyEvent->key());
//		return true;
//	}
//	else
//	{
//		// standard event processing
//		return QObject::eventFilter(obj, event);
//	}
//} 
